//
//  CaseManagerDetailController.m
//  QuickCure1
//
//  Created by zhouxuanhe on 16/11/1.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "CaseManagerDetailController.h"
#import "NetWorkTool.h"
#import "CaseManagerDetailModel.h"
#import "NSString+pinyin.h"

@interface CaseManagerDetailController ()<UISearchResultsUpdating,UISearchBarDelegate>

/****************************/

@property (nonatomic, strong) UISearchController *searchController;
@property (strong,nonatomic) NSMutableArray *searchList;//搜索结果数据

/****************************/

@property (nonatomic,strong) NSDictionary *dic;
//用来接收数据的数组
@property (nonatomic,strong) NSArray *arrayList;//全部数据



@end
static NSString *cellDetail = @"cellDetail";
@implementation CaseManagerDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _searchController=[[UISearchController alloc]initWithSearchResultsController:nil];
    //协议代理
    _searchController.searchResultsUpdater = self;
    //是否添加半透明覆盖层
    _searchController.dimsBackgroundDuringPresentation = NO;
    //是否隐藏导航栏
     _searchController.hidesNavigationBarDuringPresentation = NO;
    //设置frame
    //_searchController.searchBar.frame = CGRectMake(0, 0, 200, 40);
   _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    //添加到导航栏上
    self.navigationItem.titleView = _searchController.searchBar;
    //添加到组头上,显示出来
   //  self.tableView.tableHeaderView = self.searchController.searchBar;
    //搜索时，背景变模糊
   // _searchController.obscuresBackgroundDuringPresentation = NO;
    //将searbar显示到界面上
  //  [self.view addSubview:_searchController.searchBar];
    //设置右边取消按钮(猜的)
      [_searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
    /*这行代码是声明,哪个VC显示UISearchController,
      a.如果不添加这行代码,在设置hidesNavigationBarDuringPresentation这个属性为YES的时候,搜索框进入编辑模式会导致searchbar不可见,偏移-64; 在设置为NO的时候,进入编辑模式输入内容会导致高度为64的白条,猜测是导航栏没有渲染出来
      b.如果添加了上面这行代码,在设置hidesNavigationBarDuringPresentation这个属性为YES的时候,输入框进入编辑模式正常显示和使用,在设置为NO的时候,搜索框进入编辑模式导致向下偏移64,
     
     */
     self.definesPresentationContext = YES;
    
     
    
    
    //调用初始化方法
    [self setupUI];
}

#pragma mark -
#pragma mark - 初始化界面
-(void)setupUI
{
    //获取网络数据
    [self test4];
    
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellDetail];
}

#pragma mark - TableView data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return _arrayList.count;
    if (self.searchController.active) {
        return [self.searchList count];
    }else{
        return [self.arrayList count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellDetail forIndexPath:indexPath];
//    
//    // Configure the cell...
//    cell.textLabel.text = _arrayList[indexPath.row].ci3_name;
    
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellDetail];
    if (cell == nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellDetail];
    }
    if (self.searchController.active) {
        [cell.textLabel setText:self.searchList[indexPath.row]];
    }
    else{
        [cell.textLabel setText:self.arrayList[indexPath.row]];
    }
    return cell;
}
 
#pragma mark -
#pragma mark - 点击cell的时候调用,用来返回这个cell上内容
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //**********************************
    //发送通知或block,将疾病类型发送过去
    NSString * cellDetailString = _arrayList[indexPath.row];
    if (_BlockCaseDetail) {
        _BlockCaseDetail(cellDetailString);
    }
    
    //然后pop到上一个VC
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark - 根据接口获取网络数据 
//4. 肿瘤疾病选择疾病细分列表接口  OK
-(void)test4{
    //    请求地址: http://iosapi.itcast.cn/searchCI3List.json.php
    //    请求参数:{"page":1,"page_size":15,"ci1_id":1,"keyword":""}
    
    
    //根据对应的ID获取数据
    NSString *urlString = kSearchCI3Url;
    NSDictionary *paramDic = @{
                               @"page_size":@15,
                               @"page":@1,
                               @"ci1_id":_numType,
                               @"keyword":@""
                               };
    
    [self loadUrl:urlString params:paramDic];
}

/** 最后统一测试的 */
-(void)loadUrl:(NSString *)urlString
        params:(NSDictionary *)paramDic{
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
        NSArray *mArray = responseObject[@"data"];
        NSArray<CaseManagerDetailModel *> *lArray = [NSArray yy_modelArrayWithClass:[CaseManagerDetailModel class] json:mArray];
        //创建新的array
        NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
         [lArray enumerateObjectsUsingBlock:^(CaseManagerDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
             [mutableArray addObject:obj.ci3_name];
         }];
        _arrayList = mutableArray.copy;
        //*****
        [self.tableView reloadData];
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
    
}




#pragma mark -
#pragma mark - 搜索调用方法,更新数据
//搜索代理方法，搜索框获得第一响应或内容变化时触发
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    //实时监控输入的内容
    NSString *searchString = [self.searchController.searchBar text];
    //是否包含searchString
  //  NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    
    if (self.searchList!= nil) {
        [self.searchList removeAllObjects];
    }
    
    //符合条件的汉语数组
     NSArray *CHarray = [NSArray array];
     CHarray = [self chineseIsContainEnglish:searchString];
    
    //过滤数据
  //   self.searchList= [NSMutableArray arrayWithArray:[_arrayList filteredArrayUsingPredicate:preicate]];
      self.searchList= [NSMutableArray arrayWithArray:CHarray];
               
               
               
               
    //刷新表格
    
    [self.tableView reloadData];
}

#pragma mark -
#pragma mark - 查看一个中文词语是否包含指定的字母,并返回这个词语(数组)
-(NSArray *)chineseIsContainEnglish:(NSString *)strEnglish
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [_arrayList enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //这个时候每个元素都是汉字,先将汉字转化为拼音,判断拼音是否包含拼音,如果包含,就返回这个汉字(词语)
        //  NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", strEnglish];
        /****全拼与简拼*****/
        NSString * pinyin = [obj transformToPinyin];
        NSString *py = [obj transformToPinyinFirstLetter];
        [dic setValue:obj forKey:pinyin];
        [dic setValue:obj forKey:py];
    }];
    //用来保存key值的数组
    NSMutableArray *keyArray = [NSMutableArray array];
    [dic enumerateKeysAndObjectsUsingBlock:^(NSString *  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        [keyArray addObject:key];
    }];
    //判断key数组是否包含指定的拼音
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", strEnglish];
    //将包含指定拼音的key保存在一个新的数组里面
    NSArray *pyArray = [keyArray.copy filteredArrayUsingPredicate:preicate];
    //根据这个新的数组中的key值,取出对应的汉字(词语)
    NSMutableArray *arrCHA = [NSMutableArray array];
    [pyArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *str = dic[obj];
        //判断arrCHA数组中是否包含
        
        [arrCHA addObject:str];
    }];
    
    return arrCHA.copy;
}



//#pragma mark -
//#pragma mark - 界面销毁
//-(void)dealloc
//{
//    
//    
//}

@end
